package com.zzh.lib.views.drawer;


import android.animation.ArgbEvaluator;
import android.graphics.Paint;

import com.zzh.lib.views.banner.IndicatorOptions;
import com.zzh.lib.views.def.IndicatorSlideMode;
import com.zzh.lib.views.enums.IndicatorOrientation;

/**
 * Created by ZZH on 2023/2/16.
 *
 * @date: 2023/2/16 09:13
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.views.drawer
 */
public abstract class BaseDrawer implements IDrawer {
    public static final int INDICATOR_PADDING_ADDITION = 6;
    public static final int INDICATOR_PADDING = 3;
    private MeasureResult mMeasureResult;
    public float maxWidth = 0f;


    public float minWidth = 0f;


    public Paint mPaint = new Paint();
    public ArgbEvaluator argbEvaluator = null;

    protected IndicatorOptions mIndicatorOptions;

    public BaseDrawer(IndicatorOptions indicatorOptions) {
        mIndicatorOptions = indicatorOptions;
        initBaseDrawer();
    }

    private void initBaseDrawer() {
        mPaint.setAntiAlias(true);
        mMeasureResult = new MeasureResult();
        if (mIndicatorOptions.getSlideMode() == IndicatorSlideMode.SCALE
                || mIndicatorOptions.getSlideMode() == IndicatorSlideMode.COLOR) {
            argbEvaluator = new ArgbEvaluator();
        }
    }

    @Override
    public MeasureResult onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mIndicatorOptions.getNormalSliderWidth() > mIndicatorOptions.getCheckedSliderWidth()) {
            maxWidth = mIndicatorOptions.getNormalSliderWidth();
            minWidth = mIndicatorOptions.getCheckedSliderWidth();
        } else {
            maxWidth = mIndicatorOptions.getCheckedSliderWidth();
            minWidth = mIndicatorOptions.getNormalSliderWidth();
        }

        if (mIndicatorOptions.getOrientation() == IndicatorOrientation.INDICATOR_VERTICAL) {
            mMeasureResult.setMeasureResult(measureHeight(), measureWidth());
        } else {
            mMeasureResult.setMeasureResult(measureWidth(), measureHeight());
        }
        return mMeasureResult;
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {

    }

    public boolean isWidthEquals() {
        return mIndicatorOptions.getNormalSliderWidth() == mIndicatorOptions.getCheckedSliderWidth();
    }

    protected int measureHeight() {
        return (int) mIndicatorOptions.getSliderHeight() + INDICATOR_PADDING;
    }

    private int measureWidth() {
        int pageSize = mIndicatorOptions.getPageSize();
        float indicatorGap = mIndicatorOptions.getSliderGap();
        return ((int) ((pageSize - 1) * indicatorGap + maxWidth + (pageSize - 1) * minWidth)) + INDICATOR_PADDING_ADDITION;
    }


    public static class MeasureResult {
        int measureWidth = 0;
        int measureHeight = 0;

        public int getMeasureWidth() {
            return measureWidth;
        }

        public MeasureResult setMeasureWidth(int measureWidth) {
            this.measureWidth = measureWidth;
            return this;
        }

        public int getMeasureHeight() {
            return measureHeight;
        }

        public MeasureResult setMeasureHeight(int measureHeight) {
            this.measureHeight = measureHeight;
            return this;
        }

        public void setMeasureResult(int measureWidth, int measureHeight) {
            this.measureWidth = measureWidth;
            this.measureHeight = measureHeight;
        }
    }

}
